package com.chenxi;

import java.util.concurrent.locks.ReentrantReadWriteLock;
// 读读共享

public class MyReadLock {

    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    public void read(){
        try{
            lock.readLock().lock();     //读锁锁定
            System.out.println("获取读锁" + Thread.currentThread().getName()+ " "+ System.currentTimeMillis());
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.readLock().unlock();
        }
    }

    public static void main(String[] args) {
        MyReadLock myReadLock = new MyReadLock();
        new Thread(new Runnable() {
            @Override
            public void run() {
                myReadLock.read();
            }
        },"aaa").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                myReadLock.read();
            }
        },"bbb").start();
    }
}
